import numpy as np

ls = [1, 3.14, "张三"]
print(ls)       # [1, 3.14, '张三']

arr = np.array([1, 3.14, 5])
print(arr)      # [1.   3.14 5.  ]


print("----------------------")

ls = [1, 2, 3, 4, 5]
# print(ls + 1)   报错
ls2 = [10, 20, 30, 40, 50]
print(ls + ls2) # 输出 [1, 2, 3, 4, 5, 10, 20, 30, 40, 50]


arr1 = np.array([1, 2, 3, 4, 5])
print(arr1 + 1)
arr2 = np.array([10, 20, 30, 40, 50])
print(arr1 + arr2)  # 输出 [11 22 33 44 55]

print("-----------------------")
import time
import numpy as np

def timer(function):
    def wrapper(*args, **kwargs):
        start = time.time()     # 函数执行前记录时间
        ret = function(*args, **kwargs)
        end = time.time()       # 函数执行结束后记录时间
        print(f"函数 {function.__name__} 执行时间为 {(end - start)*1000:.4f} ms")
        return ret
    return wrapper

# 使用List实现平方运算
@timer
def list_square(lst):
    return [x ** 2 for x in lst]

# 使用Ndarray实现平方运算
@timer
def ndarray_square(arr):
    return arr ** 2

# 准备百万级的数据
data = list(range(10 ** 6))
arr = np.array(data)

print("开始测试性能对比")
list_square(data)
ndarray_square(arr)
print("测试结束")

"""
开始测试性能对比
函数 list_square 执行时间为 256.3760 ms
函数 ndarray_square 执行时间为 1.9829 ms
测试结束
"""
print("------------------------")
import numpy as np

# 标量创建
arr1 = np.array(1)
print("标量创建", arr1)

# 元组创建
arr1 = np.array((1, 2, 3, 4))
print("元组创建", arr1)

# 列表创建
arr1 = np.array([1, 2, 3, 4])
print("列表创建", arr1)


print("-----------------------------")
import numpy as np

data = [1, 2, 3, 4, 255]

arr1 = np.array(data, dtype=np.float16)
print(arr1, arr1.dtype)

arr2 = np.array(data, dtype=np.uint8)
print(arr2, arr2.dtype) # uint8 表示8位无符号二进制 取值范围是0-255

"""
C语言中的结构体
struct stu{
    char name[20];
    int age;
    float score;
};
"""
# Ndarray实现结构体数组
stu_type = [("name", "U10"), ("age", "u1"), ("score", "f2")]
stu_data = [("张三", 20, 90), ("李四", 21, 98.5), ("王五", 22, 100)]
stu_arr = np.array(stu_data, dtype=stu_type)
print(stu_arr)

